<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread></SubBread>
      <!-- <SubBread></SubBread> -->
      <!-- 筛选区 -->
      <SubFilter @filterChange="filterChange"></SubFilter>
      <!-- 测试复选框
      <XtxCheckbox v-model="isAllChecked">{{ isAllChecked }}全选</XtxCheckbox> -->
      <!-- 商品面板 + 排序加列表 -->
      <div class="goods-list">
        <SubSort @sortChange="sortChange"></SubSort>
        <!-- 列表 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id">
            <GoodsItem :goods="goods" />
          </li>
        </ul>
        <XtxInfiniteLoading
          :loading="loading"
          :finished="finished"
          @infinite="getData"
        ></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
// import SubFilter from './component/sub-filter.vue'
import SubBread from './component/sub-bread.vue'
import SubFilter from './component/sub-filter.vue'
import SubSort from './component/sub-sort.vue'
import GoodsItem from './component/goods-item.vue'
import { findSubCategoryGoods } from '@/api/category'
import { reactive, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    // const isAllChecked = ref(true)
    // 加载中
    const loading = ref(false)
    // 加载结束
    const finished = ref(false)
    const goodsList = ref([])
    const route = useRoute()
    // 请求参数
    let requestParams = reactive({
      page: 1,
      pageSize: 20
    })
    const getData = () => {
      // console.log('加载数据去吧')
      loading.value = true
      // 设置二级分类id
      requestParams.categoryId = route.params.id
      findSubCategoryGoods(requestParams).then(({ result }) => {
        // 获取数据成功
        if (result.items.length) {
          goodsList.value.push(...result.items)
          // 把page改成下一页的页码
          requestParams.page++
          // console.log(result.items.length)
        } else {
          // 没有数据加载完成
          finished.value = true
          console.log(finished)
        }
        loading.value = false
      })
    }
    // 更改了二级分类需要重新加载数据
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        finished.value = false
        goodsList.value = [] // 导致列表为空,加载更多组件进入可视区 就会加载数据
        requestParams = {
          page: 1,
          pageSize: 20
        }
      }
    })
    // 1. 更改排序组件的筛选属性,重新请求
    const sortChange = (sortParams) => {
      // console.log(sortParams)
      // 重新请求
      finished.value = false
      // 合并请求参数,保留之前的参数
      requestParams = { ...requestParams, ...sortParams }
      requestParams.page = 1
      goodsList.value = []
    }
    // 2. 更改筛选组件的筛选数据,重新请求
    const filterChange = (filterParams) => {
      // console.log(filterParams)
      finished.value = false
      // 合并请求参数
      requestParams = { ...requestParams, ...filterParams }
      requestParams.page = 1
      goodsList.value = []
    }
    return { getData, loading, finished, goodsList, sortChange, filterChange }
  }
}
</script>
<style  scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
